package per.hx.bs.sys.serviceimpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang3.StringUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import per.hx.bs.sys.dao.SysMenuDao;
import per.hx.bs.sys.dao.SysRoleDao;
import per.hx.bs.sys.dao.SysRoleMenuDao;
import per.hx.bs.sys.dao.SysUserRoleDao;

import per.hx.bs.entity.sys.SysRoleDO;
import per.hx.bs.entity.sys.SysRoleMenuDO;

import java.util.*;

/**
 * @author HuXing
 * @date 2018/3/22  11:15
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "sys-service/role")
public class SysRoleApiServiceImpl {


    private final
    SysRoleDao sysRoleDao;
    private final
    SysRoleMenuDao sysRoleMenuDao;
    private final
    SysMenuDao sysMenuDao;
    private final
    SysUserRoleDao sysUserRoleDao;

    @Autowired
    public SysRoleApiServiceImpl(SysRoleDao sysRoleDao, SysRoleMenuDao sysRoleMenuDao, SysMenuDao sysMenuDao, SysUserRoleDao sysUserRoleDao) {
        this.sysRoleDao = sysRoleDao;
        this.sysRoleMenuDao = sysRoleMenuDao;
        this.sysMenuDao = sysMenuDao;
        this.sysUserRoleDao = sysUserRoleDao;
    }


    /**
     * 获取用户角色集合
     *
     * @param listRolesId 角色id
     * @return 用户角色集合
     */
    @GetMapping(value = "/roles")
    public Set<String> listRoles(@RequestParam("listRolesId") List<Long> listRolesId) {
        Map<String, Object> listRolesIdParam = new HashMap<>(1);
        listRolesIdParam.put("list", listRolesId);
        List<String> roles = sysRoleDao.selectRoleNameByIds(listRolesIdParam);
        Set<String> roleSet = new HashSet<>();
        for (String perm : roles) {
            if (StringUtils.isNotBlank(perm)) {
                roleSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return roleSet;
    }

    /**
     * 获取 用户权限集合
     *
     * @param listRolesId 角色id
     * @return 用户权限集合
     */
    @GetMapping(value = "/perms")
    public Set<String> listPerms(@RequestParam("listRolesId") List<Long> listRolesId) {
        Map<String, Object> listRolesIdParam = new HashMap<>(1);
        listRolesIdParam.put("list", listRolesId);
        List<String> perms = sysMenuDao.selectPermsByRoleIds(listRolesIdParam);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }


    /**
     * 通过id获取角色
     *
     * @param roleId 角色Id
     * @return 角色信息
     */
    @GetMapping(value = "/{roleId}")
    public SysRoleDO getOne(@PathVariable("roleId") Long roleId) {
        return sysRoleDao.selectById(roleId);
    }

    /**
     * 创建角色
     *
     * @param sysRoleDO 角色信息
     * @return 影象的行数
     */
    @PostMapping(value = "/save")
    public int save(@RequestBody SysRoleDO sysRoleDO) {
        int count = sysRoleDao.insert(sysRoleDO);
        List<Long> menuIds = sysRoleDO.getMenuIds();
        Long roleId = sysRoleDO.getRoleId();
        Map<String, Object> map = new HashMap<>(1);
        map.put("role_id", roleId);
        sysRoleMenuDao.deleteByMap(map);

        List<SysRoleMenuDO> rms = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenuDO sysRoleMenuDO = new SysRoleMenuDO();
            sysRoleMenuDO.setMenuId(menuId);
            sysRoleMenuDO.setRoleId(roleId);
            rms.add(sysRoleMenuDO);
        }
        if (!rms.isEmpty()) {
            sysRoleMenuDao.batchInsert(rms);
        }
        return count;
    }

    /**
     * 角色信息更新
     *
     * @param sysRoleDO 角色信息
     * @return 影响的行数
     */
    @PutMapping(value = "/update")
    public int update(@RequestBody SysRoleDO sysRoleDO) {

        int count = sysRoleDao.updateAllColumnById(sysRoleDO);
        List<Long> menuIds = sysRoleDO.getMenuIds();
        Long roleId = sysRoleDO.getRoleId();
        Map<String, Object> map = new HashMap<>(1);

        map.put("role_id", roleId);
        sysRoleMenuDao.deleteByMap(map);

        List<SysRoleMenuDO> rms = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenuDO roleMenuDO = new SysRoleMenuDO();
            roleMenuDO.setRoleId(roleId);
            roleMenuDO.setMenuId(menuId);
            rms.add(roleMenuDO);
        }
        if (!rms.isEmpty()) {
            sysRoleMenuDao.batchInsert(rms);
        }
        return count;
    }

    /**
     * 删除 角色信息
     *
     * @param roleId 角色id
     * @return 影响的行数
     */
    @DeleteMapping(value = "/remove")
    public int remove(@RequestParam("roleId") Long roleId) {
        int count = sysRoleDao.deleteById(roleId);
        Map<String, Object> map = new HashMap<>(1);
        map.put("role_id", roleId);
        sysRoleMenuDao.deleteByMap(map);
        sysUserRoleDao.deleteByMap(map);
        return count;
    }

    /**
     * 批量 删除 角色信息
     *
     * @param ids 角色id
     * @return 影响的行数
     */
    @DeleteMapping(value = "/removes")
    public int batchRemove(@RequestParam("ids") Long[] ids) {
        return sysRoleDao.deleteBatchIds(Arrays.asList(ids));
    }


    /**
     * 获取角色信息
     *
     * @return 角色列表
     */
    @GetMapping(value = "/list")
    public List<SysRoleDO> list() {
        return sysRoleDao.selectByMap(new HashMap<String, Object>(0));
    }

    /**
     * 获取角色信息
     *
     * @return 角色列表
     */
    @GetMapping(value = "/list/{userId}")
    public List<SysRoleDO> list(@PathVariable("userId") Long userId) {
        List<Long> roleIds = sysUserRoleDao.listRolesId(userId);
        List<SysRoleDO> roleDOS = sysRoleDao.selectByMap(new HashMap<String, Object>(1));
        for (SysRoleDO sysRoleDO : roleDOS) {
            sysRoleDO.setRoleSign("false");
            for (Long roleId : roleIds) {
                if (Objects.equals(sysRoleDO.getRoleId(), roleId)) {
                    sysRoleDO.setRoleSign("true");
                    break;
                }
            }

        }
        return roleDOS;
    }

    /**
     * 检查标识符是够存在
     *
     * @param roleSign 标识符
     * @return 检查结果
     */
    @GetMapping(value = "/check/{roleSign}")
    public Boolean signExit(@PathVariable("roleSign") String roleSign) {
        return sysRoleDao.selectOne(new SysRoleDO(roleSign)) == null;
    }
}
